﻿using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using BizLoginHacker.Properties;

namespace BizLoginHacker
{
    class Program
    {
        private static string Account;
        private static string Employee;
        private static string Password;
        private static bool IE;

        static void Main(string[] args)
        {
            if (args.Length == 4)
            {
                Account = args[0];
                Employee = args[1];
                Password = args[2];
                IE = bool.Parse(args[3]);
            }
            else
            {
                Account = Settings.Default.Account;
                Employee = Settings.Default.Employee;
                Password = Settings.Default.Password;
                IE = Settings.Default.IE;
            }


            string serial;
            string time;
            string encryptData;
            string text = "28706";
            bool epsEnable = false;
            string epsSerial = "";
            string epsDgst = "";
            string epsPin = "";

            GetEncryptData(epsEnable, epsSerial, epsDgst, epsPin, "", text, false, out serial, out time, out encryptData);
            StringBuilder builder = new StringBuilder();
            builder.Append("http://www.800j.com");
            builder.Append("/Service/BizLogin.aspx?");
            builder.Append("Ext=" + text);
            builder.Append("&Sn=" + HttpUtility.UrlEncode(serial));
            builder.Append("&Ts=" + time);
            builder.Append("&Et=1");
            string str8 = HttpUtility.UrlEncode(encryptData);
            builder.Append("&Data=" + str8);
            builder.Append("&Ver=" + VersionString);
            builder.Append("&Qm=" + MD5(str8));

            StartBrowser(builder.ToString());
        }

        private static void StartBrowser(string p)
        {
            var chrome = string.Format(
                @"{0}\Google\Chrome\Application\chrome.exe",
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
            if (File.Exists(chrome) && !IE)
                Process.Start(chrome, p);
            else
                Process.Start("iexplore", p);
        }

        private static string MD5(string value)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(value, "md5").ToLower();
        }

        public static string VersionString
        {
            get
            {
                return "7.4.0.0";
            }
        }

        private static void GetEncryptData(bool epsEnable, string epsSerial, string epsDgst, string epsPin, string dpsPwd, string extCode, bool isBind, out string serial, out string time, out string encryptData)
        {
            serial = GetSerial();
            if (string.IsNullOrEmpty(serial))
            {
                serial = DateTime.Now.Ticks.ToString();
            }
            else
            {
                serial = serial.Replace(" ", "").Replace("<", "").Replace(">", "");
                serial = HttpUtility.UrlEncode(serial);
                if (serial.Length > 15)
                {
                    serial = serial.Substring(serial.Length - 15, 15);
                }
            }
            time = DateTime.Now.Ticks.ToString();
            string str = (time + serial + extCode + extCode + extCode).Substring(0, 0x18);
            StringBuilder builder = new StringBuilder();
            builder.Append("zh=" + HttpUtility.UrlEncode(Account));
            builder.Append("&em=" + HttpUtility.UrlEncode(Employee));
            builder.Append("&pw=" + HttpUtility.UrlEncode(Password));
            string[] strArray = GetMacAddressesFake();
            if ((strArray != null) && (strArray.Length > 0))
            {
                builder.Append("&mc=" + HttpUtility.UrlEncode(strArray[0]));
                string str2 = "";
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (i > 0)
                    {
                        str2 = str2 + ",";
                    }
                    str2 = str2 + strArray[i].ToString();
                }
                builder.Append("&mmc=" + HttpUtility.UrlEncode(str2));
            }
            //builder.Append("&ps=" + HttpUtility.UrlEncode(xae5ca8a9e667960a(extCode)));
            builder.Append("&ps=" + HttpUtility.UrlEncode((extCode)));
            //if (isBind)
            //{
            //    builder.Append("&sps=" + HttpUtility.UrlEncode(this.x19e41b257f2548dc(extCode)));
            //}
            //if (epsEnable)
            //{
            //    builder.Append("&eb=1");
            //    builder.Append("&es=" + HttpUtility.UrlEncode(epsSerial));
            //    builder.Append("&ed=" + HttpUtility.UrlEncode(epsDgst));
            //    builder.Append("&ep=" + HttpUtility.UrlEncode(epsPin));
            //}
            builder.Append("&dp=" + dpsPwd);
            //if ((!string.IsNullOrEmpty(this.x75274c39ece90e79.Text) && !string.IsNullOrEmpty(this.x1551f08994fe0cf9.Text)) && !string.IsNullOrEmpty(this.xc8567897cd286f64.Text))
            //{
            //    builder.Append("&jn1=" + this.xd87a65b90252e732.ToString());
            //    builder.Append("&jc1=" + this.x75274c39ece90e79.Text);
            //    builder.Append("&jn2=" + this.x46c37447e3f17843.ToString());
            //    builder.Append("&jc2=" + this.x1551f08994fe0cf9.Text);
            //    builder.Append("&jn3=" + this.x6102722629d8c901.ToString());
            //    builder.Append("&jc3=" + this.xc8567897cd286f64.Text);
            //}
            builder.Append("&v=" + VersionString);
            encryptData = EncryptData(builder.ToString(), str, "", PaddingMode.Zeros, 0x18, "ToHex16");
            //if (x6b6c7e50e19a7af0.IsDebugEnabled)
            //{
            //    x6b6c7e50e19a7af0.Debug("key=" + str + " srcData=" + builder.ToString() + " encryptData=" + encryptData);
            //}
        }


        public static string GetSerial()
        {
            string value = "";
            try
            {
                ManagementClass class2 = new ManagementClass("Win32_DiskDrive");
                foreach (ManagementObject obj2 in class2.GetInstances())
                {
                    value = (string)obj2.Properties["Model"].Value;
                }
                class2 = null;
                value = value.TrimEnd(new char[1]);
            }
            catch
            {
            }
            if (string.IsNullOrEmpty(value))
            {
                value = GetSerialEx();
            }
            return value.Replace("\n", "").TrimEnd(new char[1]);
        }

        public static string GetSerialEx()
        {
            try
            {
                new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObject obj2 = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
                obj2.Get();
                return obj2.GetPropertyValue("VolumeSerialNumber").ToString();
            }
            catch
            {
                return "";
            }
        }


        public static string[] GetMacAddressesFake()
        {
            //return GetMacAddresses();
            return new string[] { "00-58-F1-AC-1C-F3" };
            //return new string[] { "00-58-F1-AC-1C-F7" };
        }

        public static string[] GetMacAddresses()
        {
            try
            {
                ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                string str = string.Empty;
                int num = 0;
                foreach (ManagementObject obj2 in instances)
                {
                    if (((bool)obj2["IPEnabled"]) && (str.IndexOf(obj2["MacAddress"].ToString()) < 0))
                    {
                        if (num > 0)
                        {
                            str = str + ";";
                        }
                        str = str + obj2["MacAddress"].ToString();
                        num++;
                    }
                    obj2.Dispose();
                }
                return str.Replace(":", "-").Split(new char[] { ';' });
            }
            catch
            {
                return new string[] { "unknow" };
            }
        }

        public static string EncryptData(string x59876d0c8467fe39, string x39d9653625493ae8, string x3bf23781e017dee4, PaddingMode x27268e0b4e0c959c, int x19fb5132643a0897, string xf9dd6040b0ad84a8)
        {
            string str2;
            if (string.IsNullOrEmpty(xf9dd6040b0ad84a8))
            {
                throw new Exception("输出配置不能空");
            }
            if (x39d9653625493ae8.Trim().Length != x19fb5132643a0897)
            {
                throw new Exception("密钥字节长度不对");
            }
            try
            {
                SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
                byte[] bytes = Encoding.Default.GetBytes(x39d9653625493ae8);
                algorithm.Key = bytes;
                if (string.IsNullOrEmpty(x3bf23781e017dee4))
                {
                    x3bf23781e017dee4 = x39d9653625493ae8.Substring(0, 8);
                }
                algorithm.IV = Encoding.Default.GetBytes(x3bf23781e017dee4);
                algorithm.Mode = CipherMode.ECB;
                algorithm.Padding = x27268e0b4e0c959c;
                ICryptoTransform transform = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
                byte[] buffer = Encoding.Default.GetBytes(x59876d0c8467fe39.Trim());
                string str = string.Empty;
                using (MemoryStream stream = new MemoryStream())
                {
                    CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.FlushFinalBlock();
                    stream2.Close();
                    if (xf9dd6040b0ad84a8 == "ToHex16")
                    {
                        str = xd2025a0160cd3e23(stream.ToArray());
                    }
                    if (xf9dd6040b0ad84a8 == "ToBase64")
                    {
                        str = Convert.ToBase64String(stream.ToArray()).ToString().Replace("\0", "");
                    }
                }
                str2 = str;
            }
            catch (Exception exception)
            {
                throw new Exception("加密发生错误,原因：" + exception.Message, exception);
            }
            return str2;
        }



        public static string xd2025a0160cd3e23(byte[] xf9a0d04800d70471)
        {
            if ((xf9a0d04800d70471 == null) || (xf9a0d04800d70471.Length == 0))
            {
                return "";
            }
            char[] chArray = new char[xf9a0d04800d70471.Length * 2];
            for (int i = 0; i < xf9a0d04800d70471.Length; i++)
            {
                int num2 = xf9a0d04800d70471[i];
                chArray[i * 2] = xd02417b130c4e0d0[num2 >> 4];
                chArray[(i * 2) + 1] = xd02417b130c4e0d0[num2 & 15];
            }
            return new string(chArray);
        }
        public static char[] xd02417b130c4e0d0 = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };


    }

}
